<?php
namespace app\app\controller\v2;
use app\app\Appbase;
use think\facade\Session;
use think\facade\Cache;

class Shop extends Appbase 
{
	
	/**
	* 我的店铺
	* /app/v2/shop/myShop
	* 需要登录 
	* @is_need_login true
	*/
	public function myShop(){
		try {
			$result = model('Shop')->getShopInfo(request() ->shopId);
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 我的钱包
	* /app/v2/shop/myWallet
	* 需要登录 
	* @is_need_login true
	*/
	public function myWallet(){
		try {
			$result = model('Shop')->getShopBill(request() ->shopId);
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 装修
	* /app/v2/shop/edit
	* 需要登录 
	* @is_need_login true
	* @method post
	*/
	public function edit($img='',$bgimg='',$certificate=''){
		try {
			if($img && $bgimg && $certificate){
				model('Shop')->where('id', request() ->shopId )->update([
					'img'=>$img,
					'bgimg'=>$bgimg,
					'certificate_photo'=>$certificate
				]);
			}else if($img && $bgimg){
				model('Shop')->where('id', request() ->shopId )->update([
					'img'=>$img,
					'bgimg'=>$bgimg
				]);
			}else if($img){
				model('Shop')->where('id', request() ->shopId )->update(['img'=>$img]);
			}else if($bgimg){
				model('Shop')->where('id', request() ->shopId )->update(['bgimg'=>$bgimg]);
			}else if($certificate){
				model('Shop')->where('id', request() ->shopId )->update([
					'certificate_photo'=>$certificate
				]);
			}
			
			return $this->ReSucess();	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 首页视图
	* /app/v2/shop/indexView
	* 需要登录 
	* @is_need_login true
	* @method post
	*/
	public function indexView(){
		try {
			$result = model('Shop')->indexView( request() ->shopId );
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 数据中心
	* /app/v2/shop/dataView
	* 需要登录 
	* @is_need_login true
	* @method post
	*/
	public function dataView($type=1,$date=''){
		try {
			// TODO 营业额
			if($type==1 || !$type){
	       		$turnover = model('Shop')->turnover( request() ->shopId,$date );
			}
			//TODO 订单数据
			if($type==2 || !$type){
	        	$order =  model('Shop')->orderView( request() ->shopId,$date );
			}
			// TODO 访客量
			if($type==3 || !$type){
		        $visitor = model('Shop')->visitor( request() ->shopId );
			}
			//返回
			return $this->ReSucess(compact('turnover','order','visitor') );	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 提现
	* /app/v2/shop/withdraw
	* 需要登录 
	* @is_need_login true
	* @method post
	*/
	public function withdraw(){
		$request =  request();
		$shopInfo = $request ->shopInfo;
		try {
         	$result = model('ShopExtract')->shopExtract($shopInfo,$request->post());
			if($result === false){
				return $this->ReError( model('ShopExtract')->errorInfo );
			}
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 提现记录
	* /app/v1/user/withdraw
	* 是否需要登录 
	* @is_need_login true
	* @method post 
	*/
	public function withdrawLog($pageIndex=1,$pageSize=20){
		$id = request() ->shopInfo['id'];
		try {
			//TODO 提现列表
	        $extractList = model('ShopExtract')->extractList($id,$pageIndex,$pageSize);
			//TODO 累积已提现
	        $extractSum =  model('ShopExtract')->extractSum($id);
			//TODO 获得用户提现总金额-不论成败
	        $extractTotal =  model('ShopExtract')->userExtractTotalPrice($id);
			//----
			return $this->ReSucess(compact('extractList','extractSum','extractTotal') );	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
}



